package ch15;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*set 判断重复：首先根据hashCode判断相同，再看eauqls()
字符串如果相同，hashCode相同(即需要重写equals()方法和hashCode()方法)
*/
public class SetTest2 {
    public static void main(String[] args) {
        Set set= new HashSet();
        set.add(new BankAccountSet("hf-1","丽丽","5467","南京"));
        set.add(new BankAccountSet("hf-2","小敏","111","北京"));
        set.add(new BankAccountSet("hf-3","王刚","45611","湖北"));

        set.add(new BankAccountSet("hf-3","小王","156","天津"));
        System.out.println("个数"+set.size());//4个
        //set 判断重复：首先根据hashCode判断相同（如果hashCode不一样那么就不会再看equals()了），再看equals()
        //字符串如果相同，hashCode相同(字符传中hashCode=char*char)

        //重写hashCode()方法（equals()方法也重写，根据no来判断账号是否相同）后结果为：3个
        for(Object o:set){
            BankAccountSet b=(BankAccountSet)o;
            System.out.println("账号："+b.getNo()+",姓名："+b.getName()+",身份证件号："
                    +b.getIdCard()+",城市："+b.getCity());
        }

        System.out.println("******迭代一次********");//迭代器只能使用一次
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());//要想实现输出，需要BankAccountSet中重写toString()方法。
        }



    }
}
